<?php
	require_once "conn.php";
	$t=time();
	$now_time=date("Y-m-d",$t);
	$user_id=$_GET['userid'];
	$book_id=$_GET['bookid'];
	
	//执行减一本书
	$sql="select * from book where id=".$book_id;
	$result = $conn->query($sql);
	$row=mysqli_fetch_assoc( $result );
	if($row['数量']>0)
	{
		$num=$row["数量"]-1;
		$sql="update book set 数量=".$num." where id=".$book_id;
		if ($conn->query($sql) === TRUE) {
   	 	echo "数量更新成功";
		} else {
   		 echo "Error: " . $sql . "<br>" . $conn->error;
		}
		
		//借阅图书
		$sql = "INSERT INTO jie (`借书时间`, `图书编号`, `用户编号`)
		VALUES ('".$now_time."',".$book_id.",".$user_id.")";

		if ($conn->query($sql) === TRUE) {
   	 	echo "新记录插入成功";
		} else {
   		 echo "Error: " . $sql . "<br>" . $conn->error;
		}
	}
	
	$conn->close();
	header('Location:/index.php');
?>